const connection = require("../app/database");

class userService {
  // 创建用户
  async create(user) {
    const { name, password } = user; // 获取用户名和密码

    const statement = "insert into `user` (`name`,`password`) values (?,?);";

    const [result] = await connection.execute(statement, [name, password]);

    return result;
  }

  // 根据用户名查询用户
  async findUserByName(name) {
    const statement = "select * from `user` where `name` = ?;";

    const [values] = await connection.execute(statement, [name]);

    return values;
  }

  // 更新用户头像
  async updateUserAvatar(avatarUrl, userId) {
    const statement = `UPDATE user SET avatar_url = ? where id = ?;`;
    const [result] = await connection.execute(statement, [avatarUrl, userId]);
    return result;
  }
}

module.exports = new userService();
